package com.alei.redis.redisdemo.config;

import com.alei.redis.redisdemo.controller.SysMenuController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;


/**
 * @author ShiLei
 * @version v1.0
 * @date 2019-11-21 15:14
 */
@EnableScheduling
@Component
@Slf4j
public class RedisStatusTask {

    @Resource
    private JedisPool jedisPool;

    @Autowired
    public RedisStatusTask(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    @Scheduled(cron = "0/5 * * * * ? ")
    public void checkStatus() {
        log.info("每隔五秒去检查 redis 状态是否可用~~");
        try (Jedis jedis = jedisPool.getResource()) {
            String ping = jedis.ping();
            if (!StringUtils.isEmpty(ping)) {
                SysMenuController.FLAG = true;
            }
        } catch (Exception e) {
            //发代办
        }
    }
}
